//
// Created by gogoqiu on 2022/2/14.
//

#include "MainApp.h"

IMPLEMENT_APP_CONSOLE(ConsoleApp);

//https://forums.wxwidgets.org/viewtopic.php?t=18843
class AnOtherThread : public wxThread{
public:
    AnOtherThread( wxUint16 uint_sleep_second = 3){
        this->sleep_second = uint_sleep_second;
    }
    void * Entry() override;
    virtual int Run();
private:
    wxUint16 sleep_second;
};

void * AnOtherThread::Entry(){
    return nullptr;
}

int AnOtherThread::Run(){
    wxSleep( sleep_second );
    wxCommandEvent event( ID_THREAD_EXIT, wxEVT_USER_FIRST );
    wxGetApp().AddPendingEvent( event );
    //wxLogMessage( wxT("Info"));
    return wxThread::Run();
}

int ConsoleApp::FilterEvent( wxEvent& event){
    if(wxEVT_USER_FIRST == event.GetEventType()){
        ExitMainLoop();
        return true;
    }
    return -1; //need further token care
}

bool ConsoleApp::OnInit(){
    auto * thread = new AnOtherThread;
    if( thread->Create() != wxTHREAD_NO_ERROR )
    {
        //there are some errors when creating new thread
        delete thread;
        return false;
    }
    thread->Run();
    //thread created successfully
    return true;
}

int ConsoleApp::OnExit(){
    return 0;
}
